package com.weiiew.common.redis;

import org.springframework.core.convert.ConversionService;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;

/**
 * @Description
 * @Author 魏锴
 * @Date 2025/4/6
 */
public class fuzzyRedisCache extends RedisCache {
    private final String name;
    private final RedisCacheWriter cacheWriter;
    private final ConversionService conversionService;
    public fuzzyRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration configuration){
        super(name, cacheWriter, configuration);
        this.name = name;
        this.cacheWriter = cacheWriter;
        this.conversionService = configuration.getConversionService();
    }
    @Override
    public void evict(Object key){
        if (key instanceof String && ((String) key).contains("*")){
            // 处理通配符逻辑
            byte[] pattern = conversionService.convert(createCacheKey(key), byte[].class);
            cacheWriter.clean(getName(), pattern);  // 调用clean方法进行模糊删除
        }
        else super.evict(key);
    }
}
